package org.cloud.mybatisplus.controller.admin;

import com.github.pagehelper.PageInfo;
import com.unknow.first.mongo.vo.DataInterFaceVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Map;
import org.cloud.constant.CoreConstant.AuthMethod;
import org.cloud.context.RequestContextManager;
import org.cloud.dimension.annotation.SystemResource;
import org.cloud.mybatisplus.mybatis.dynamic.DynamicSqlUtil;
import org.cloud.mybatisplus.utils.AuthCheckUtils;
import org.cloud.mybatisplus.vo.DynamicSqlQueryParamsVO;
import org.cloud.mybatisplus.vo.JavaBeanResultMap;
import org.cloud.vo.CommonApiResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 动态Sql接口执行controller
 */
@Api(value = "DataInterfaceExecController", tags = "管理员：动态SQL执行接口")
@RestController
@RequestMapping("/admin/dynamic/sql")
@SystemResource(path = "/admin/dynamic/sql")
public class DataInterfaceAdminExecController {
    @ApiOperation(value = "分页查询", notes = "分页查询,传入接口的MD5值")
    @PostMapping("/paged/{md5}/{pageNum}/{pageSize}")
    @SystemResource(value = "datainterface_admin_query", description = "管理员：调用查询动态接口查询", authMethod = AuthMethod.BYUSERPERMISSION)
    public CommonApiResult<PageInfo<JavaBeanResultMap>> paged(@PathVariable("md5") String md5, @PathVariable("pageNum") int pageNum,
        @PathVariable("pageSize") int pageSize, @RequestBody DynamicSqlQueryParamsVO dynamicSqlQueryParamsVO) throws Exception {
        AuthCheckUtils.single().checkDataInterface(md5);
        return CommonApiResult.createSuccessResult(DynamicSqlUtil.single().pagedData(md5, pageNum, pageSize, dynamicSqlQueryParamsVO));
    }

    @ApiOperation(value = "不分页查询", notes = "分页查询,传入接口的MD5值")
    @SystemResource(value = "datainterface_admin_query", description = "管理员：调用查询动态接口查询", authMethod = AuthMethod.BYUSERPERMISSION)
    public CommonApiResult<JavaBeanResultMap> list(@PathVariable("md5") String md5,
        @RequestBody DynamicSqlQueryParamsVO dynamicSqlQueryParamsVO) throws Exception {
        AuthCheckUtils.single().checkDataInterface(md5);
        return CommonApiResult.createSuccessResult(DynamicSqlUtil.single().listData(md5, dynamicSqlQueryParamsVO));
    }

}
